package com.ruoyi.project.jiaqin.question.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import com.ruoyi.project.jiaqin.followmember.domain.JiaqinFollowMember;
import com.ruoyi.project.jiaqin.followmember.mapper.JiaqinFollowMemberMapper;
import com.ruoyi.project.jiaqin.question.domain.JiaqinQuestion;
import com.ruoyi.project.jiaqin.question.mapper.JiaqinQuestionMapper;
import com.ruoyi.project.jiaqin.question.service.IJiaqinQuestionService;
import com.ruoyi.project.jiaqin.questionlikes.domain.MemberQuestionLikes;
import com.ruoyi.project.jiaqin.questionlikes.mapper.MemberQuestionLikesMapper;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * 问题Service业务层处理
 *
 * @author admin
 * @date 2020-12-30
 */
@Service
public class JiaqinQuestionServiceImpl implements IJiaqinQuestionService
{
    @Autowired
    private JiaqinQuestionMapper jiaqinQuestionMapper;
    @Autowired
    private MemberQuestionLikesMapper memberQuestionLikesMapper;
    @Autowired
    private JiaqinFollowMemberMapper jiaqinFollowMemberMapper;

    /**
     * 查询问题
     *
     * @param id 问题ID
     * @return 问题
     */
    @Override
    public JiaqinQuestion selectJiaqinQuestionById(String id)
    {
        return jiaqinQuestionMapper.selectJiaqinQuestionById(id);
    }

    /**
     * 查询问题列表
     *
     * @param jiaqinQuestion 问题
     * @return 问题
     */
    @Override
    public List<JiaqinQuestion> selectJiaqinQuestionList(JiaqinQuestion jiaqinQuestion)
    {
        return jiaqinQuestionMapper.selectJiaqinQuestionList(jiaqinQuestion);
    }


    @Autowired
    private IMemberService memberService;
    @Autowired
    private ICompanyService companyService;
    @Override
    public PageInfo<JiaqinQuestion> selectJiaqinQuestions(Integer pageNum,Integer pageSize,JiaqinQuestion jiaqinQuestion,String userId) {
        PageHelper.startPage(pageNum,pageSize);
        List<JiaqinQuestion> list = jiaqinQuestionMapper.selectJiaqinQuestionList(jiaqinQuestion);
        if(list != null && list.size()!=0){
            for (JiaqinQuestion j:list) {
                JiaqinFollowMember jiaqinFollowMember = jiaqinFollowMemberMapper.selectJiaqinFollowMemberByMemberIdAndToMemberId(userId,j.getMemberId());
                if(jiaqinFollowMember != null){
                    j.setFollow(true);
                }else{
                    j.setFollow(false);
                }
                // 根据,切割图片字符串 变成List
                if(j.getImgUrls() != null && !j.getImgUrls().equals("")) {
                    List<String> split = Arrays.asList(j.getImgUrls().split(","));
                    j.setQuestionImg(split);
                }
                // 是否是企业
                if (j.getMemberId() != null) {
                    Member memberById = memberService.selectMemberById(j.getMemberId());
                    if (memberById != null) {
                        Company company = companyService.selectCompanyByMemberId(j.getMemberId());
                        if (company != null) {
                            j.setIsCompany(1);// 企业
                        } else {
                            j.setIsCompany(0);// 个人
                        }
                    }
                }
            }
        }
        PageInfo<JiaqinQuestion> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     *
     * 新增问题
     *
     * @param jiaqinQuestion 问题
     * @return 结果
     */
    @Override
    public int insertJiaqinQuestion(JiaqinQuestion jiaqinQuestion)
    {
        jiaqinQuestion.setId(UUID.randomUUID().toString().replace("-",""));
        return jiaqinQuestionMapper.insertJiaqinQuestion(jiaqinQuestion);
    }

    /**
     * 修改问题
     *
     * @param jiaqinQuestion 问题
     * @return 结果
     */
    @Override
    public int updateJiaqinQuestion(JiaqinQuestion jiaqinQuestion)
    {
        return jiaqinQuestionMapper.updateJiaqinQuestion(jiaqinQuestion);
    }

    /**
     * 删除问题对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJiaqinQuestionByIds(String ids)
    {
        return jiaqinQuestionMapper.deleteJiaqinQuestionByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除问题信息
     *
     * @param id 问题ID
     * @return 结果
     */
    @Override
    public int deleteJiaqinQuestionById(String id)
    {
        return jiaqinQuestionMapper.deleteJiaqinQuestionById(id);
    }

    @Override
    @Transactional
    public int likesQuestion(String memberId, String questionId) {
        MemberQuestionLikes memberQuestionLikes = new MemberQuestionLikes();
        memberQuestionLikes.setMemberId(memberId);
        memberQuestionLikes.setQuestionId(questionId);
        memberQuestionLikesMapper.insertMemberQuestionLikes(memberQuestionLikes);
        return jiaqinQuestionMapper.updateLikes(1L,questionId);
    }

    @Override
    @Transactional
    public int unlikesQuestion(String memberId, String questionId) {
        memberQuestionLikesMapper.deleteMemberQuestionLikesById(memberId,questionId);
        return jiaqinQuestionMapper.updateLikes(-1L,questionId);
    }
}
